/******************************************************************
 * Created by: GOH CHEE HONG                                      
 * Date of Creation: 1/AUG/2008     
 * Date of last modification: 
 *                                                                
 * Description: BUDDIES' KEY MANAGEMENT
 ******************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Pbdms.Common.CryptoUtils;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.Encoders;
using Pbdms.Common.DataObject;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Tools
{
    public partial class FrmKeyManager : newForms
    {
        private ArrayList arlBuddies;
        public delegate void DelegateWorker(object sender, KeyVerifyResponse e);
        
        public FrmKeyManager()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
            initHeader();
            trustheader();
        }
        /// <summary>
        /// customize the GUI
        /// </summary>
        private void initHeader()
        {
            grdvPublicKeys.ColumnCount = 5;

            grdvPublicKeys.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            grdvPublicKeys.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            grdvPublicKeys.ColumnHeadersDefaultCellStyle.Font =
                new Font(grdvPublicKeys.Font, FontStyle.Bold);

            grdvPublicKeys.Name = "grdvPublicKeys";
   
            grdvPublicKeys.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            grdvPublicKeys.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;
            grdvPublicKeys.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            grdvPublicKeys.GridColor = Color.Black;
            grdvPublicKeys.RowHeadersVisible = false;

            grdvPublicKeys.Columns[0].Name = "Name";
            grdvPublicKeys.Columns[0].Width = 100;
            grdvPublicKeys.Columns[1].Name = "Key ID";
            grdvPublicKeys.Columns[1].Width = 120;
            grdvPublicKeys.Columns[2].Name = "Trust";
            grdvPublicKeys.Columns[2].Width = 50;
            grdvPublicKeys.Columns[3].Name = "Fingerprint";
            grdvPublicKeys.Columns[3].Width =240;
            grdvPublicKeys.Columns[4].Name = "User Info";
            grdvPublicKeys.Columns[4].Width = 400;
            
           /*
            grdvPublicKeys.Columns[4].DefaultCellStyle.Font =
                new Font(grdvPublicKeys.DefaultCellStyle.Font, FontStyle.Italic);
            */
            grdvPublicKeys.SelectionMode =
                DataGridViewSelectionMode.FullRowSelect;
            grdvPublicKeys.MultiSelect = true;
            grdvPublicKeys.AutoResizeColumns();

            grdvPublicKeys.CellClick += new DataGridViewCellEventHandler(grdvPublicKeys_CellClick);
            this.Activated += new EventHandler(FrmKeyManager_Activated);
            this.FormClosing += new FormClosingEventHandler(FrmKeyManager_FormClosing);
            this.btnAddPub.Click += new EventHandler(btnAddPub_Click);
            this.btnDelPub.Click += new EventHandler(btnDelPub_Click);
            this.btnSignPub.Click+=new EventHandler(signing_process);
            this.btnVerifyPub.Click+=new EventHandler(btnVerifyPub_Click);
            this.btnView.Click+=new EventHandler(btnView_Click);

        }

        /// <summary>
        /// change name
        /// </summary>
        /// <param name="keyID"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public override void changeName(string keyID, string oldName, string newName)
        {
            DataGridViewRowCollection rows = grdvPublicKeys.Rows;
            int count = rows.Count;
            for (int i = 0; i < count; i++)
            {
                DataGridViewRow row = rows[i];
                if(row.Cells[0].Value.Equals(oldName)){
                    row.Cells[0].Value = newName;
                }
            }
            
        }

        /// <summary>
        /// customize the GUI
        /// </summary>
        private void trustheader()
        {

            grdvResult.ColumnCount = 4;

            grdvResult.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            grdvResult.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            grdvResult.ColumnHeadersDefaultCellStyle.Font =
                new Font(grdvResult.Font, FontStyle.Bold);

            grdvResult.Name = "grdvPublicKeys";

            grdvResult.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            grdvResult.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;
            grdvResult.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            grdvResult.GridColor = Color.Black;
            grdvResult.RowHeadersVisible = false;

            grdvResult.Columns[0].Name = "Key that is verified";
            grdvResult.Columns[0].Width = 100;
            grdvResult.Columns[1].Name = "IP source";
            grdvResult.Columns[1].Width = 120;
            grdvResult.Columns[2].Name = "Link";
            grdvResult.Columns[2].Width = 400;
            grdvResult.Columns[3].Name = "Trust Path";
            grdvResult.Columns[3].Width = 400;
        
            /*
             grdvPublicKeys.Columns[4].DefaultCellStyle.Font =
                 new Font(grdvPublicKeys.DefaultCellStyle.Font, FontStyle.Italic);
             */
            grdvPublicKeys.SelectionMode =
                DataGridViewSelectionMode.FullRowSelect;
            grdvPublicKeys.MultiSelect = false;
            grdvPublicKeys.AutoResizeColumns();
        }
        void FrmKeyManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        void FrmKeyManager_Activated(object sender, EventArgs e)
        {
            try
            {
                int i = grdvPublicKeys.CurrentRow.Index;
                if (i != -1)
                {
                    string trusted = grdvPublicKeys.Rows[i].Cells[2].FormattedValue.ToString();

                    if (trusted.Equals("True"))
                    {

                        btnSignPub.Text = "Untrust A Friend";
                    }
                    else if (trusted.Equals("False"))
                    {
                        btnSignPub.Text = "Trust A Friend";
                    }
                }
            }
            catch
            {
              
            }
        }

        void grdvPublicKeys_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = grdvPublicKeys.CurrentRow.Index;
                if (i != -1)
                {
                    string trusted = grdvPublicKeys.Rows[i].Cells[2].FormattedValue.ToString();

                    if (trusted.Equals("True"))
                    {
                        btnSignPub.Text = "Untrust A Friend";
                    }
                    else if (trusted.Equals("False"))
                    {
                        btnSignPub.Text = "Trust A Friend";
                    }
                }
            }
            catch
            {

            }
        }

  
        /// <summary>
        /// add a key to the grid
        /// </summary>
        /// <param name="name">name of the user</param>
        /// <param name="key">key id</param>
        /// <param name="fingerprint">fingerprint</param>
        /// <param name="userInfo">user infos</param>
        /// <param name="trust">trust level</param>
        public void addKey( string name,string key, string fingerprint, string userInfo, Boolean trust)
        {
            string [] row={name,key,trust.ToString(),fingerprint,userInfo};
            grdvPublicKeys.Rows.Add(row);
        }
        #region  key related function
        /// <summary>
        /// add a public key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddPub_Click(object sender, EventArgs e)
        {

            bool alreadyExist = false;
            foreach (Form frm in (this.MdiParent.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Tools.FrmAddFriend)))
                {
                    frm.WindowState = FormWindowState.Normal;
                    frm.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Tools.FrmAddFriend frmAddFriend = new Pbdms.Forms.Tools.FrmAddFriend();
                frmAddFriend.MdiParent = this.MdiParent;
                frmAddFriend.Show();
            }



        }
        //delete a public key
        private void btnDelPub_Click(object sender, EventArgs e)
        {
           
            try
            {
                string key = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();
                string buddyName = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[0].FormattedValue.ToString();
                try
                {
                   


                    //delet gui then data
                    grdvPublicKeys.Rows.RemoveAt(grdvPublicKeys.CurrentRow.Index);
                    //delet the status
                    Program.mainForm.frmBuddiesStatus.peerTrackingManager.deleteATracker(key);
                    
                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnKeyIDFromDB(key);
                    //delete from databasse
                    BusinessLogic.DataController.BuddyController.create().deleteBuddyFromDB(buddy);
                    //delet a friend from shared control
                    Program.friendControl.deleteFriend(buddy);
                    //clear the public key
                    Program.publicKeyControl.deleteKey(key);
                    //restate the user name
                    Program.mainForm.frmInbox.deleteMessageAssociatedByKeyID(key);

                    //remove subscription
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)Program.mainForm.frmfeeds.subscManager.arrSubscriptions[key +" Newest Library additions"];
                    if (subscription != null)
                    {
                        Program.mainForm.frmfeeds.subscManager.removeSubscription(subscription);

                    }
                
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                Common.Global.showMsgOneButton("Please select a public key", 'E');
            }
        }
        private void signing_process(object sender, EventArgs e)
        {
            if (btnSignPub.Text.Equals("Trust A Friend"))
            {
                publickey_signing();
            }
            else
            {
                publickey_removing();
            }
        }
        /// <summary>
        /// sign a public key
        /// </summary>
        private void publickey_signing()
        {
            try
            {
                string pubKeyID = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();
                string buddyName = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[0].FormattedValue.ToString();
                PgpPrivateKey priKey = Program.keyControl.readPrivateKey();
                PgpSecretKey secKey = Program.keyControl.readSecretKey();
                Signer signGen = new Signer();
                MessageBox.Show("By signing a public key with your secret key, you are certifying that this public key is trustworthy");
                DialogResult result = MessageBox.Show("Do you want to sign this key " + pubKeyID + "\n with this Secret key " + priKey.KeyId.ToString("X") + "?", "Important", MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    

                    try
                    {

                        PgpPublicKey newPublicKey = signGen.SignPublicKey(priKey, secKey, Program.publicKeyControl.getPublicKey(pubKeyID), "keyID", pubKeyID);
                        if (newPublicKey != null)
                        {
                            

                            //update buddy trust
                           
                            Program.friendControl.setABuddyTrustworthy(pubKeyID, true);
                            Program.mainForm.frmBuddiesStatus.moveTreeNode(pubKeyID,"untrusted","offline");

                            Program.publicKeyControl.AddAPublicKey(newPublicKey);
                            grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[2].Value = true;

                            BusinessLogic.PeerMS.PeerTracker peer =(BusinessLogic.PeerMS.PeerTracker) Program.mainForm.frmBuddiesStatus.peerTrackingManager.HPeerTrackers[pubKeyID];
                            peer.setTrusted(true);
                            btnSignPub.Text = "Untrust A Friend";
                            MessageBox.Show("The key is signed now");
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.Global.showMsgOneButton("An unknown error occurs.\nThe operation is aborted", 'E');
                    }
                }
                else if (result == System.Windows.Forms.DialogResult.No)
                {
                    Common.Global.showMsgOneButton("You've cancelled the operation", 'I');
                    return;
                }
            }
            catch (Exception ex)
            {
                Common.Global.showMsgOneButton("Either Public key or Secret key selected is invalid", 'E');
            }
        }
        /// <summary>
        /// verify a public key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnVerifyPub_Click(object sender, EventArgs e)
        {
            frmOption frmOption = new frmOption("Please select which way to verify", "via local machine", "via trusted peers");
            DialogResult result = frmOption.ShowDialog();
            Truster truster = null;
            string pubKey = "";
            if (result == DialogResult.OK)
            {
                if (frmOption.choice == 1)
                {
                   
                    try
                    {
                        pubKey = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();

                       
                        truster = new Truster(Program.publicKeyControl);
                        
                        try
                        {
                            Queue<string> que = new Queue<string>();
                            ///queue to store the result
                            Boolean check = truster.checkTrust(Program.publicKeyControl.getPublicKey(pubKey),Program.keyControl.readSecretKey(),BusinessLogic.DataController.BuddyController.create().getListOfBuddies(), ref que);
                            if (check == true)
                            {

                                Pbdms.Common.GUI.frmKeyInformation frmTrust = null;
                                Boolean alreadyExist = false;
                                foreach (Form frm in (this.MdiParent.MdiChildren))
                                {
                                    frm.WindowState = FormWindowState.Normal;
                                    if (frm.GetType().Equals(typeof(Pbdms.Common.GUI.frmKeyInformation)))
                                    {
                                        frmTrust = (Pbdms.Common.GUI.frmKeyInformation)frm;
                                        frmTrust.clear();
                                        frmTrust.WindowState = FormWindowState.Normal;
                                        frmTrust.Activate();
                                        alreadyExist = true;
                                        break;
                                    }
                                }
                                if (!alreadyExist)
                                {
                                    frmTrust = new Pbdms.Common.GUI.frmKeyInformation();
                                    frmTrust.MdiParent = this.MdiParent;
                                    frmTrust.Show();
                                }
                                string path = "";
                                que.Dequeue();
                                string last = "You";
                                while (que.Count != 0)
                                {
                                    string whichKey = que.Dequeue();
                                    string id = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(whichKey);
                                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(id);
                                    string aPath = last + " have signed on " + buddy.Name + "'s key (" + whichKey + ")\n";
                                    frmTrust.addTrust(aPath);
                                    path = path + aPath;
                                    last = buddy.Name + "'s key (" + whichKey + ")\n";
                                }

                            }
                            else
                            {
                                Common.Global.showMsgOneButton("This key " + pubKey + " is not verified by any of our existing keys", 'W');
                            }
                        }
                        catch (Exception ex)
                        {
                            Common.Global.showMsgOneButton(ex.Message, 'E');
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.Global.showMsgOneButton("Please select a valid public key", 'E');
                    }
                }
                else
                {
                    try
                    {
                        pubKey = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();
                        grdvResult.Rows.Clear();
                        Program.networkCtrller.sendKeyToBeVerifyRequest(pubKey);

                    }
                    catch (Exception ex)
                    {

                        Common.Global.showMsgOneButton("Please select a valid public key", 'E');
                    }


                }
            }
            else
            {

            }

        }
        /// <summary>
        /// remove a public key
        /// </summary>
        private void publickey_removing()
        {
            try
            {
                string pubKeyID = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();
                string buddyName = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[0].FormattedValue.ToString();
                PgpSecretKey secKey = Program.keyControl.readSecretKey();

                MessageBox.Show("By removing your signature, this public key is not trustworthy");
                DialogResult result = MessageBox.Show("Do you want to remove your signature of this key " + pubKeyID, "Important", MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    
                    Signer signRemover = new Signer();
                    PgpPublicKey newPublicKey = signRemover.removeSignature(Program.publicKeyControl.getPublicKey(pubKeyID), secKey.PublicKey);

                    Program.publicKeyControl.AddAPublicKey(newPublicKey);
                    grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[2].Value = false;

                   
                  
                    BusinessLogic.PeerMS.PeerTracker peer = (BusinessLogic.PeerMS.PeerTracker)Program.mainForm.frmBuddiesStatus.peerTrackingManager.HPeerTrackers[pubKeyID];
                    peer.setTrusted(false);
                    Program.mainForm.frmBuddiesStatus.moveTreeNode(newPublicKey.KeyId.ToString("X"),"all","untrusted");
                    Program.friendControl.setABuddyTrustworthy(pubKeyID, false);
                    btnSignPub.Text = "Trust A Friend";
                    MessageBox.Show("Signature removed successfully");
                }
                else if (result == System.Windows.Forms.DialogResult.No)
                {
                    Common.Global.showMsgOneButton("You've cancelled the operation", 'I');
                    return;
                }
            }
            catch (Exception ex)
            {
                Common.Global.showMsgOneButton("An error occurred.\nThe operation is aborted", 'E');
            }
        }
        #endregion
        #region old code

        public void loadPublicKeys()
        {

            arlBuddies = new ArrayList();
            arlBuddies = BusinessLogic.DataController.BuddyController.create().getListOfBuddies();

            
          
                     
        }
     
        private void loadResultsHeader()
        {
            DataTable dt = new DataTable();
            //add user name, key, fingerprint,user id  and no of signatures trust columns
            for (int i = 0; i < 4; i++)
            {
                dt.Columns.Add();
            }
            grdvResult.DataSource = dt;
            for (int i = 0; i < 4; i++)
            {
                switch (i)
                {
                    case 0: grdvResult.Columns[0].HeaderText = "Key that is verified"; grdvResult.Columns[0].ReadOnly = true; break;
                    case 1: grdvResult.Columns[1].HeaderText = "IP Source"; grdvResult.Columns[1].ReadOnly = true; break;
                    case 2: grdvResult.Columns[2].HeaderText = "Nodes"; grdvResult.Columns[2].ReadOnly = true; break;
                    case 3: grdvResult.Columns[3].HeaderText = "Trust Path"; grdvResult.Columns[3].ReadOnly = true; break;
                  
                }

            }
        }
      
  
     

        private void customizeGridViewForPublicKeys()
        {
            for (int i = 0; i < 6; i++)
            {
                switch (i)
                {
                    case 0: grdvPublicKeys.Columns[0].HeaderText = "Name"; grdvPublicKeys.Columns[0].ReadOnly = true; break;
                    case 1: grdvPublicKeys.Columns[1].HeaderText = "Key Id"; grdvPublicKeys.Columns[1].ReadOnly = true;  break;
                    case 2: grdvPublicKeys.Columns[2].HeaderText = "Fingerprint"; grdvPublicKeys.Columns[2].ReadOnly = true;  break;
                    case 3: grdvPublicKeys.Columns[3].HeaderText = "User info";  grdvPublicKeys.Columns[3].ReadOnly = true; break;
                    case 4: grdvPublicKeys.Columns[4].HeaderText = "No of signatures"; grdvPublicKeys.Columns[4].ReadOnly = true; break;
                    case 5: grdvPublicKeys.Columns[5].HeaderText = "trustworthy"; grdvPublicKeys.Columns[5].ReadOnly = true; break;
                }

            }
        }
        
        





        #region public keys button event handlers
        

       
        #endregion
       


        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
        }
        /// <summary>
        /// view more detail information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnView_Click(object sender, EventArgs e)
        {
            
            try
            {
                string key = grdvPublicKeys.Rows[grdvPublicKeys.CurrentRow.Index].Cells[1].FormattedValue.ToString();
                bool alreadyExist = false;
                foreach (Form frm in (this.MdiParent.MdiChildren))
                {
                    frm.WindowState = FormWindowState.Normal;
                    if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmDetailKey)))
                    {
                        frm.WindowState = FormWindowState.Normal;
                        Pbdms.Forms.Views.FrmDetailKey detailKey = (Pbdms.Forms.Views.FrmDetailKey)frm;
                        detailKey.setNewKey(key);
                        detailKey.Activate();
                        alreadyExist = true;
                        break;
                    }
                }
                if (!alreadyExist)
                {
                    Pbdms.Forms.Views.FrmDetailKey frmDetailKey = new Pbdms.Forms.Views.FrmDetailKey(key);
                    frmDetailKey.MdiParent = this.MdiParent;
                    frmDetailKey.Show();
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            grdvResult.Columns.Clear();
            loadResultsHeader();
        }

        /// <summary>
        /// method that handle incoming key result
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void addItem(object sender, KeyVerifyResponse e)
        {



            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker MessageWorker = new DelegateWorker(addItem);

                    this.BeginInvoke(MessageWorker, new object[] { sender, e });

                }
                else
                {
                    string[] temp = new string[4];
                    temp[0] = e.keyToBeVerifyID;
                    temp[1] = e.ipAddSource;
                    temp[2] = e.link;
                    temp[3] = e.trustpath;

                    grdvResult.Rows.Add(temp);
                }
            }
            catch
            {

            }
        }


        
       
        #endregion

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Hide();
        }

        //set all untrusted
        public void setALlUntrusted()
        {
            int i= grdvPublicKeys.Rows.Count;
            for (int j = 0; j < i; j++)
            {
                grdvPublicKeys.Rows[j].Cells[2].Value = false;
            }
            btnSignPub.Text = "Trust A Friend";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiParent.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmChangeName)))
                {
                   
                    Pbdms.Forms.Views.FrmChangeName frmChangeName = (Pbdms.Forms.Views.FrmChangeName)frm;
                    frmChangeName.MdiParent = this.MdiParent;
                    frmChangeName.setNewName((string)grdvPublicKeys.SelectedRows[0].Cells[0].Value, (string)grdvPublicKeys.SelectedRows[0].Cells[1].Value);
                    frmChangeName.WindowState = FormWindowState.Normal;
                    frmChangeName.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Views.FrmChangeName frmChangeName= new Pbdms.Forms.Views.FrmChangeName();
                frmChangeName.MdiParent = this.MdiParent;
                frmChangeName.setNewName((string)grdvPublicKeys.SelectedRows[0].Cells[0].Value, (string)grdvPublicKeys.SelectedRows[0].Cells[1].Value);
                frmChangeName.Show();
            }
 
        }
 
    }
}